package com.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.common.constant.BaseConst;
import com.common.baseModel.JsonResult;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ClassName ServletUtils
 * @Description TODO
 * @Author 吃麻椒的花酱
 * @Date 2022/2/9 20:42
 * @Version 1.0
 */
public class ServletUtils {
    /**
     * 获取RequestAttributes对象
     * @return
     */
    public static ServletRequestAttributes getRequestAttributes(){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes)attributes;
    }

    /**
     * 获取request对象
     * @return
     */
    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    /**
     * 获取response对象
     * @return
     */
    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }

    /**
     * 获取session对象
     * @return
     */
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    /**
     * 把字符串打印到客户端
     * @param string
     * @return
     */
    public static String sendString(HttpServletResponse response, String string) {
        try {
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  设置WebFlux模型
     * @param response http response
     * @param msg
     * @return
     */

    public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String msg) {
        return webFluxResponseWriter(response, HttpStatus.OK, msg, BaseConst.ResCode.ERROR);
    }

    /**
     *
     * @param response
     * @param ok
     * @param msg
     * @param error
     * @return
     */
    private static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus ok, String msg, Integer error) {
        response.setStatusCode(ok);
        response.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        JsonResult<Object> result = JsonResult.createError(msg);
        DataBuffer dataBuffer = response.bufferFactory().wrap(JSONObject.toJSONString(result).getBytes());
        return response.writeWith(Mono.just(dataBuffer));
    }
}
